import {createRouter, createWebHistory} from 'vue-router'
import Table1 from '../views/Table1.vue'
import Table2 from '../views/Table2.vue'
import Table3 from '../views/Table3.vue'
import Table4 from '../views/Table4.vue'
import Table5 from '../views/Table5.vue'
import login from '../views/login.vue'
import home from '../views/home.vue'
import LogLogin from '@/views/LogLogin.vue'
import LogOperation from '@/views/LogOperation.vue'
import ExcelUpload from '../views/ExcelUpload.vue'
import UserManagement from "@/views/UserManagement.vue";
import request from "@/utils/request";
import CompanySearch from "@/views/CompanySearch.vue";
import SongDaJuChuan from "@/views/SongDaJuChuan.vue";
import GuanLianRenSearch from "@/views/GuanLianRenSearch.vue";
import Clean from "@/views/Clean.vue";
import Company from "@/views/Company.vue";
import TopPage from '../views/TopPage.vue';
import companyQCC from '../views/companyQCC.vue';
import KG from "@/views/KG.vue";
import GuanliXiezuo from '@/views/GuanliXiezuo.vue'
import TestAPI from "@/views/TestAPI.vue";

const routes = [{
    path: '/', name: 'login', component: login
}, {
    path: '/home',
    name: 'home',
    component: home,
    children: [{path: '/manage1', name: 'manage1', component: Table1, meta: {requiresAuth: true}}, {
        path: '/manage2',
        name: 'manage2',
        component: SongDaJuChuan,
        meta: {requiresAuth: true}
    }, {path: '/manage3', name: 'manage3', component: GuanLianRenSearch, meta: {requiresAuth: true}}, {
        path: '/manage4',
        name: 'manage4',
        component: Clean,
        meta: {requiresAuth: true}
    }, {path: '/manage5', name: 'manage5', component: Table5, meta: {requiresAuth: true}}, {
        path: '/manage6',
        name: 'manage6',
        component: ExcelUpload,
        meta: {requiresAuth: true}
    }, // 添加用户管理路由
        {path: '/manage7', name: 'manage7', component: UserManagement, meta: {requiresAuth: true}}, {
            path: '/manage8',
            name: 'manage8',
            component: LogLogin,
            meta: {requiresAuth: true}
        }, {path: '/manage9', name: 'manage9', component: LogOperation, meta: {requiresAuth: true}}, {
            path: '/manage10',
            name: 'manage10',
            component: Company,
            meta: {requiresAuth: true}
        }, {
            path: '/data-summary',
            name: 'DataSummary',
            component: () => import('../views/DataSummary.vue'),
            meta: {requiresAuth: true}
        },
        {
            path: '/yugu',
            name: 'YuGu',
            component:  () => import('../views/Tableyugu.vue'),
            meta: {requiresAuth: true}
        },
        {
            path: '/yugu_company',
            name: 'YuGuCompany',
            component:  () => import('../views/Tableyugucompany.vue'),
            meta: {requiresAuth: true}
        },
        {
            path: '/TopPage',
            name: 'TopPage',
            component: TopPage,
            meta: {requiresAuth: true}
        },
        {
            path: '/companyQCC',
            name: 'companyQCC',
            component: companyQCC,
            meta: {requiresAuth: true}
        },
        {
            path: '/GuanliXiezuo',
            name: 'GuanliXiezuo',
            component: GuanliXiezuo,
            meta: {requiresAuth: true}
        },
        {
            path: '/test',
            name: 'test',
            component: TestAPI,
            meta: {requiresAuth: true}
        }
    ]
},

]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL), routes
})

router.beforeEach(async (to, from, next) => {
    // // 假设有一个方法来检查用户是否登录
    // const isAuthenticated = false;
    //
    // if (to.matched.some(record => record.meta.requiresAuth)) {
    //     // 这条路由需要登录
    //     if (!isAuthenticated) {
    //         // 用户未登录，重定向到登录页面
    //         next({
    //             path: '/login',
    //             query: {redirect: to.fullPath} // 将目标路由作为参数，登录后重定向
    //         });
    //     } else {
    //         // 用户已登录，继续执行
    //         next();
    //     }
    // } else {
    //     // 不需要登录的路由，直接通过
    //     next();
    // }
    // 检查路由是否需要认证
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // // 需要认证的路由
        // try {
        //     // 调用后端接口检查用户是否已登录
        //     const response = await request.get('/user/info');
        //     if (response.code === 200) {
        //         // 用户已登录，允许访问
        //         next();
        //     } else {
        //         // 用户未登录，重定向到登录页面
        //         next({
        //             path: '/', query: {redirect: to.fullPath}
        //         });
        //     }
        // } catch (error) {
        //     // 请求失败，重定向到登录页面
        //     next({
        //         path: '/', query: {redirect: to.fullPath}
        //     });
        // }
        next();
    } else {
        // 不需要认证的路由，直接通过
        next();
    }
});
export default router
